<template>
    <div>
        <el-row :gutter="10">
            <el-col :span="6">
                <dol-tree
                        title="<%= modelNameText %>树"
                        :url="url.tree"
                        @current-change="showDetail"
                        ref="tree">
                    <el-button slot="button" type="text" @click="add">
                        <i class="el-icon-plus"></i>
                    </el-button>
                </dol-tree>
            </el-col>
            <el-col :span="18">
                <el-card>
                    <div slot="header" class="clearfix">
                        <span><%= modelNameText %>信息</span>
                        <span class="card-button">
                            <el-button slot="button" type="text" @click="addChild" :disabled="!detailForm._id">
                                <i class="el-icon-plus"></i>
                            </el-button>
                            <el-button slot="button" type="text" @click="edit" :disabled="!detailForm._id">
                                <i class="el-icon-edit"></i>
                            </el-button>
                            <el-button slot="button" type="text" @click="removeOne" :disabled="!detailForm._id">
                                <i class="el-icon-delete"></i>
                            </el-button>
                        </span>
                    </div>
                    <el-form :model="detailForm" label-width="80px" size="small" class="detailForm">
                        <el-row :gutter="10">
                            <% var count = 0, key; %>
                            <% for (key in field){if(!unDetail[key]){ %>
                            <el-col :span="12">
                                <el-form-item label="<%= nameFormatter[key] || key %>">
                                    {{detailForm.<%= key %>}}
                                </el-form-item>
                            </el-col>
                            <% }} %>
                        </el-row>
                    </el-form>
                </el-card>
            </el-col>
        </el-row>

        <el-dialog
                title="<%= modelNameText %>维护"
                top="5vh"
                :visible.sync="editDialog">
            <el-form ref="form" :model="editForm" label-width="80px" size="small">
                <el-row :gutter="10">
                    <% var count = 0, key; %>
                    <% for (key in field){if(!unEditor[key]){ %>
                    <el-col :span="12">
                        <el-form-item label="<%= nameFormatter[key] || key %>">
                            <el-input v-model="editForm.<%= key %>"></el-input>
                        </el-form-item>
                    </el-col>
                    <% }} %>
                </el-row>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialog = false" size="small">取 消</el-button>
                <el-button type="primary" @click="save" size="small">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                url: {
                    find: '<%= path.api %><%= path.route %>/find',
                    tree: '<%= path.api %><%= path.route %>/tree',
                    removeOne: '<%= path.api %><%= path.route %>/remove/{id}',
                    save: '<%= path.api %><%= path.route %>/save/{id}',
                    detail: '<%= path.api %><%= path.route %>/detail/{id}',
                    importData: '<%= path.api %><%= path.route %>/import',
                    exportData: '<%= path.api %><%= path.route %>/export',
                },
                editDialog: false,
                editForm: {},
                detailForm: {},
                formId: '',
            };
        },
        computed: {},
        methods: {
            query() {
                this.$refs.tree.query();
            },
            add() {
                this.editForm = {};
                this.formId = '';
                this.editDialog = true;
            },
            addChild() {
                this.editForm = {
                    parent: this.detailForm
                };
                this.formId = '';
                this.editDialog = true;
            },
            edit() {
                this.editForm = Object.assign({}, this.detailForm);
                this.formId = this.detailForm._id;
                this.editDialog = true;
            },
            save() {
                this.$ajax({
                    url: this.url.save,
                    method: 'post',
                    data: this.editForm,
                    pathData: {id: this.formId},
                }).then(reData => {
                    this.editDialog = false;
                    this.$message(reData.message);
                    this.detailForm = this.editForm;
                    this.query();
                });
            },
            removeOne() {
                this.$ajax({
                    url: this.url.removeOne,
                    pathData: {id: this.detailForm._id},
                }).then((reData) => {
                    this.$message(reData.message);
                    this.query();
                    this.detailForm = {};
                });
            },
            showDetail(data, node) {
                this.detailForm = data;
            },
        },
        created() {
        }
    }
</script>